var postsData = require('../../../data/posts-data.js');
var app = getApp();
Page({
  data: {
    isPlayingMusic: false
  },
  onLoad: function(option) {
    var postId = option.id;
    this.data.currentPostId = postId;
    var postData = postsData.postList[postId];
    this.setData({
      postData: postData
    })
    //是否被收藏的状态，默认为不被收藏
    //读取对象的属性
    //判断是够被选中用collected
    var postsCollected = wx.getStorageSync("posts_collected");
    if (postsCollected) {
      var postCollected = postsCollected[postId]
      this.setData({
        collected: postCollected
      })
    } else {
      var postsCollected = {} //定义一个变量，
      postsCollected[postId] = false; //当前的文章状态为false
      wx.setStorageSync('posts_collected', postsCollected) //放到缓存里

    }
    if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId === postId ){
        this.setData({
          isPlayingMusic : true
        })
    }
    this.setMusicMonitor();
  },

  setMusicMonitor:function(){
      var that = this;
      wx.onBackgroundAudioPlay(function(){
          that.setData({
            isPlayingMusic : true
          })
          app.globalData.g_isPlayingMusic = true;
          app.globalData.g_currentMusicPostId = that.data.currentPostId;
      })
      wx.onBackgroundAudioPause(function(){
        that.setData({
          isPlayingMusic : false
        })
        app.globalData.g_isPlayingMusic = false;
        app.globalData.g_currentMusicPostId = null;
      })
    wx.onBackgroundAudioStop(function () {
      that.setData({
        isPlayingMusic: false
      })
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_currentMusicPostId = null;
    })
  },
  onCollectionTap: function() {
    this.getPostsCollectedSyc();
    //this.getPostsCollectedAsy();

  },
  //异步方法
  getPostsCollectedAsy: function() {
    var that = this;
    wx.getStorage({
      key: 'posts_collected',
      success: function(res) {
        var postsCollted = res.data;
        //通过data为中转，在onload里定义，获取postId
        var postCollected = postsCollected[that.data.currentPostId];
        postCollected = !postCollected;
        postsCollected[that.data.currentPostId] = postCollected;
        that.showToast(postsCollected, postCollected);
      },
    })
  },
  //同步方法
  getPostsCollectedSyc: function() {
    //获取缓存
    var postsCollected = wx.getStorageSync('posts_collected');
    //通过data为中转，在onload里定义，获取postId
    var postCollected = postsCollected[this.data.currentPostId];
    //取反操作，收藏变未收藏，未收藏变收藏
    postCollected = !postCollected;
    // postsCollected[this.data.currentPostId] = postCollected;
    // //更新文章是否被收藏的缓存值
    // wx.setStorageSync('posts_collected', postsCollected);
    // //更新数据绑定变量，实现图片切换功能
    // this.setData({
    //   collected: postCollected
    // })
    postsCollected[this.data.currentPostId] = postCollected;
    this.showToast(postsCollected, postCollected);
  },
  showModal:function(){
    var that = this;
  wx.showModal({
    title: '收藏',
    content: postCollected ? "收藏该文章":"取消收藏改文章？",
    showCancel: true,
    cancelText: '取消',
    cancelColor: '#333',
    confirmText: '确认',
    confirmColor: '#405f80',
    success: function(res) {
      if(res.confirm){
        wx.setStorageSync('posts_collected', postsCollected)
      }
    },
  })
  },
  showToast: function(postsCollected, postCollected) {
    //更新文章是否有缓存值
    wx.setStorageSync('posts_collected', postsCollected);
    //更新数据绑定变量，实现切换图片
    this.setData({
      collected: postCollected
    })
    wx.showToast({
      title: postCollected ? "收藏成功" : "取消成功",
      duration: 1000,
      icon: "success"
    })
  },
  onshareTap:function(event){
    var itemList = [
      "分享给微信好友",
      "分享到朋友圈",
      "分享到QQ",
      "分享到微博"
    ];
    wx.showActionSheet({
      itemList: itemList,
      itemColor:"#405f88",
      success:function(res){
        wx.showModal({
          title: '用户'+itemList[res.tapIndex],
         content: '用户是否取消？'+res.cancel + "现在无法实现分享功能。。。",
        })
      }
    })
  },
  onMusicTap:function(event){
    var currentPostId = this.data.currentPostId;
    var postData = postsData.postList[currentPostId];
    var isPlayingMusic = this.data.isPlayingMusic;
    if (isPlayingMusic){
        wx.pauseBackgroundAudio();
        this.setData({
          isPlayingMusic:false
        })
    }else{
      wx.playBackgroundAudio({
        dataUrl: postData.music.url,
        title:postData.music.title,
        coverImgUrl:postData.music.coverImg,
      })
      this.setData({
        isPlayingMusic:true
      })
      app.globalData.g_currentMusicPostId = this.data.currentPostId;
      app.globalData.g_isPlayingMusic = true;
    }
  },
})